const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


//数据转化
function formatNumber2(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 时间戳转化为年 月 日 时 分 秒
 * number: 传入时间戳
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致
*/
function formatTime2(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber2(date.getMonth() + 1));
  returnArr.push(formatNumber2(date.getDate()));

  returnArr.push(formatNumber2(date.getHours()));
  returnArr.push(formatNumber2(date.getMinutes()));
  returnArr.push(formatNumber2(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

function sele(time){
   //a是获取的日期，b是当前日期
    // 获取当前时间戳
    var currentTime = parseInt(new Date().getTime());
    var diffTime = currentTime - time;
    console.log(currentTime);
  console.log(currentTime);
  console.log(diffTime);
    console.log(time);
    var second = 0;
    var minute = 0;
    var hour = 0;
    if (null != diffTime && "" != diffTime) {
      if (diffTime > 60 && diffTime < 60 * 60) {
        diffTime = parseInt(diffTime / 60.0) + "分钟" + parseInt((parseFloat(diffTime / 60.0) - parseInt(diffTime / 60.0)) * 60) + "秒";
      }
      else if (diffTime >= 60 * 60 && diffTime < 60 * 60 * 24) {
        diffTime = parseInt(diffTime / 3600.0) + "小时" + parseInt((parseFloat(diffTime / 3600.0) -
          parseInt(diffTime / 3600.0)) * 60) + "分钟" +
          parseInt((parseFloat((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60) -
            parseInt((parseFloat(diffTime / 3600.0) - parseInt(diffTime / 3600.0)) * 60)) * 60) + "秒";
      }
      else {
        //超过1天
        var date = new Date(parseInt(time) * 1000);
        diffTime = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
        //diffTime = parseInt(diffTime) + "秒";
      }
    }
    return diffTime;
}


function dateStr(date) {
  let selfTime =date,   // 补全为13位  PHP时间戳为10位
    oneselfDate = new Date(selfTime),
    diffValue = new Date().getTime() - selfTime,
    monthC = diffValue / 2592000000,// 月1000 * 60 * 60 * 24 * 30
    weekC = diffValue / 604800000,  // 周1000 * 60 * 60 * 24 * 7
    dayC = diffValue / 86400000,    // 天1000 * 60 * 60 * 24
    hourC = diffValue / 3600000,    // 时1000 * 60 * 60
    minC = diffValue / 60000;       // 分1000 * 60
  return diffValue < 0 ? '不久前' :// 如果本地时间反而小于变量时间
    monthC > 12 ? (oneselfDate.getFullYear() + '年' + Array(2 - ('' + oneselfDate.getMonth() + 1).length + 1).join(0) + (oneselfDate.getMonth() + 1) + '月' + Array(2 - ('' + oneselfDate.getDate()).length + 1).join(0) + oneselfDate.getDate() + '日') : // 超过1年，直接显示年月日
      monthC >= 1 ? (parseInt(monthC) + '月前') :
        weekC >= 1 ? (parseInt(weekC) + '周前') :
          dayC >= 1 ? (parseInt(dayC) + '天前') :
            hourC >= 1 ? (parseInt(hourC) + '小时前') :
              minC >= 1 ? (parseInt(minC) + '分钟前') : '刚刚';
}




module.exports = {
  formatTime: formatTime,
  formatTime2: formatTime2,
  sele: sele,
  dateStr: dateStr
}
